package com.yjd.bi.etl;

import java.util.List;

import org.apache.log4j.Logger;

import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.service.IDataChangeRecordTrsService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.concurrent.task.ITaskHandler;
import com.yjd.comm.base.concurrent.task.ITaskItem;
import com.yjd.comm.base.concurrent.task.TaskDisposeUtil;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.job.BaseJob;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.FrameUtil;

/**
 * <b>description</b>：将业务库的数据变更日志移到bi库 <br>
 * <b>time</b>：2016-4-8下午3:47:51 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class MoveDataChageRecordFromBsToBiJob extends BaseJob {

	private Logger logger = Logger
			.getLogger(MoveDataChageRecordFromBsToBiJob.class);

	@SuppressWarnings("unchecked")
	@Override
	protected boolean doExecute() throws Exception {
		List<BiDatabaseInfoModel> databaseList = this.biDatabaseInfoService
				.getModelList(FrameUtil.newHashMap("is_elt_to_bi", 1),
						DbWREnums.READ);
		TaskDisposeUtil.dispose(true, 5, databaseList,
				new ITaskHandler<BiDatabaseInfoModel>() {
					@Override
					public void dispose(ITaskItem<BiDatabaseInfoModel> item) {
						String itemJson = FrameUtil.json(FrameUtil.newHashMap(
								"id", item.getItem().getId(), "url", item
										.getItem().getJdbc_url(),
								"description", item.getItem().getDescription()));
						try {
							MoveDataChageRecordFromBsToBiJob.this.logger
									.info("将业务库的数据变更日志移到bi库 start:" + itemJson);
							MoveDataChageRecordFromBsToBiJob.this.dataChangeRecordTrsService
									.moveDcrmFromBusDsToBiDs(item.getItem());
							MoveDataChageRecordFromBsToBiJob.this.logger
									.info("将业务库的数据变更日志移到bi库 end:" + itemJson);
						} catch (Exception e) {
							MoveDataChageRecordFromBsToBiJob.this.logger
									.error("MoveDataChageRecordFromBsToBiJob.moveDcrmFromBusDsToBiDs 数据出错,BiDatabaseInfoModel:"
											+ itemJson);
							MoveDataChageRecordFromBsToBiJob.this.logger.error(
									"MoveDataChageRecordFromBsToBiJob.moveDcrmFromBusDsToBiDs 数据出错,"
											+ e.getMessage(), e);
						}
					}
				});
		return true;
	}

	@Override
	protected String getJobType() {
		return MoveDataChageRecordFromBsToBiJob.class.getName();
	}

	/**
	 * 数据变更日志迁移服务接口
	 */
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IDataChangeRecordTrsService dataChangeRecordTrsService;

	/**
	 * 数据库信息服务
	 */
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiDatabaseInfoService biDatabaseInfoService;
}
